/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-11-29     73554       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#define DBG_TAG "W25Qxx"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#include <fal.h>
#include "drv_spi.h"
#include "spi_flash_sfud.h"

extern void Peripheral_power_ctrl(int  powermode); //flash供电

#define W25Q_SPI_DEVICE_NAME     "spi10"

/*********************************************************************************/

/*********************************************************************************/
/* 挂载设备到SPI总线，并向内核注册SPI设备
  *  设备为 spi10，即SPI1总线上的第0号设备
  *  设置SPI片选引脚
 * */
int w25q_spi_device_init()
{
    __HAL_RCC_GPIOA_CLK_ENABLE();
    return rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);
}
INIT_DEVICE_EXPORT(w25q_spi_device_init);
/*********************************************************************************/

/*********************************************************************************/
/* 通过SFUD驱动库和SPI设备探测SPI Flash
 * */
static int rt_hw_spi_flash_with_sfud_init(void)
{
    Peripheral_power_ctrl(1);
    if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10"))
    {
        return RT_ERROR;
    };

    return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init);
/*********************************************************************************/

///*********************************************************************************/
//
///*********************************************************************************/
//void w25q128_mount(void)
//{
//    rt_device_t dev;
//
//    dev = rt_device_find("W25Q128");
//    if(dev != RT_NULL)
//    {
//        if(dfs_mount("W25Q128", "/", "elm", 0, 0) == 0)
//        {
//            LOG_D(" spi_flash mount to spi succ!\n");
//        }
//        else
//        {
//            LOG_D(" spi_flash mount to spi failed!\n");
//         }
//    }
//}
////INIT_APP_EXPORT(w25q128_mount);
///*********************************************************************************/

///*********************************************************************************/
//static void spi_w25q_sample(int argc, char *argv[])
//{
//    struct rt_spi_device *spi_dev_w25q;
//    char name[RT_NAME_MAX];
//    rt_uint8_t w25x_read_id = 0x90;
//    rt_uint8_t id[5] = {0};
//
//    if(argc == 2)
//    {
//        rt_strncpy(name, argv[1], RT_NAME_MAX);
//    } else
//    {
//        rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
//    }
//
//    spi_dev_w25q = (struct rt_spi_device *)rt_device_find(name);
//    if(!spi_dev_w25q)
//    {
//        LOG_D("spi sample run failed! can't find %s device!\n", name);
//    }
//    else
//    {
//        struct rt_spi_configuration cfg;
//        cfg.data_width = 8;
//        cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */
//        cfg.max_hz = 50 * 1000 * 6 ; /* 50M */
//        rt_spi_configure(spi_dev_w25q, &cfg);
//
//        rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5);
//        LOG_D("use rt_spi_send_then_recv() read w25q ID is %x%x\n", id[3], id[4]);
//
//        struct rt_spi_message msg1, msg2;
//        msg1.send_buf = &w25x_read_id;
//        msg1.recv_buf = RT_NULL;
//        msg1.length = 1;
//        msg1.cs_take = 1;
//        msg1.cs_release = 0;
//        msg1.next = &msg2;
//
//        msg2.send_buf = RT_NULL;
//        msg2.recv_buf = id;
//        msg2.length = 5;
//        msg2.cs_take = 0;
//        msg2.cs_release = 1;
//        msg2.next = RT_NULL;
//
//        rt_spi_transfer_message(spi_dev_w25q, &msg1);
//        LOG_D("use rt_spi_transfer_mesage() read w25q ID is :%x%x\n", id[3], id[4]);
//    }
//}
//MSH_CMD_EXPORT(spi_w25q_sample, spi_w25q_sample samole);
///*********************************************************************************/

///*********************************************************************************/
//#include <dfs_fs.h>
////#define DBG_TAG "user_dfs_port"
////#define DBG_LVL DBG_LOG
////#include <rtdbg.h>
//
//#define FS_PARTITION_NAME "datafile"
//int dfs_mount_init(void)
//{
//    /* 在 spi flash 中名为 "filesystem" 的分区上创建一个块设备 */
//    struct rt_device *flash_dev = fal_blk_device_create(FS_PARTITION_NAME);
//    if (flash_dev == NULL)
//    {
//        LOG_E("Can't create a block device on '%s' partition.", FS_PARTITION_NAME);
//    }
//    else
//    {
//        LOG_D("Create a block device on the %s partition of flash successful.", FS_PARTITION_NAME);
//    }
//
//    /* 挂载 spi flash 中名为 "filesystem" 的分区上的文件系统 */
//    if (dfs_mount(flash_dev->parent.name, "/", "elm", 0, 0) == 0)
//    {
//        LOG_I("Filesystem initialized!");
//    }
//    else
//    {
//        LOG_E("Failed to initialize filesystem!");
//        LOG_D("Create a filesystem on the block device !");
//        int res=dfs_mkfs("elm", flash_dev->parent.name);
//        if(res==0)
//        {
//            LOG_D("Create success !");
//            LOG_D("reboot ... !");
//            rt_thread_delay(200);
//            NVIC_SystemReset();//重启设备，由于已格式化，所以重启直接能够挂载成功
//        }
//        else {
//            LOG_D("Create fail !");
//        }
//    }
//
//    return 0;
//}
///*********************************************************************************/

/*********************************************************************************/

/*********************************************************************************/






